<template>
  <page-header-wrapper :title="false">
    <a-card class="content-card">
      <a-table 
        size="middle"
        :columns="columns" 
        :data-source="data" 
        :rowKey="r=> r.id" 
      >
        <template slot="action" slot-scope="r">
          <template v-if="r.id==2 || r.parentId==2">
            <a @click="handleAddClick(r)">新增</a>
            <a-divider type="vertical"></a-divider>
          </template>
          <a @click="handleEditorClick(r)">修改</a>
          <template v-if="r.id!=5 && r.parentId">
            <a-divider type="vertical"></a-divider>
            <a-popconfirm
              title="您确定要删除吗?"
              ok-text="确定"
              cancel-text="取消"
              @confirm="hanldeDelete(r)"
            >
              <a>删除</a>
            </a-popconfirm>
          </template>
        </template>
      </a-table>
    </a-card>
    <!-- 新增 -->
    <CreateModal ref="createModal" :parent="this"></CreateModal>
  </page-header-wrapper>
</template>

<script>
import { getMenuTree,deleteMenu } from '@/api/manage'
import { showMessage } from '@/utils/mixins'
import CreateModal from './modules/CreateModal.vue'

export default {
  name: 'WebMenu',
  mixins: [showMessage],
  components: {
    CreateModal
  },
  data() {
    return {
      columns: [
        { title: '菜单名称', dataIndex: 'title', key: 'title' },
        { title: '排序', dataIndex: 'sortNum', key: 'sortNum',align:'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, align: 'left', fixed: 'right',width: 160 },
      ],
      data: []
    }
  },
  methods: {
    // 删除列表
    hanldeDelete(r) {
      deleteMenu({id: r.id}).then(res => {
        this.showMessage(res,this.getList)
      })
    },
    // 新增
    handleAddClick(r) {
      this.$refs.createModal.form.parentId = r.id
      this.$refs.createModal.visible = true
      this.$refs.createModal.status = 1
    },
    // 修改
    handleEditorClick(r) {
      this.$refs.createModal.status = 2
      this.$refs.createModal.visible = true
      this.$refs.createModal.id = r.id
      const _form = this.$refs.createModal.form
      for(const key in _form) {
        _form[key] = r[key]
      }
      // 这是系列带图片的菜单
      if(r.imageUrl) {
        this.$refs.createModal.fileList = [
          {
            name: r.title,
            uid: r.id,
            url: r.imageUrl.includes('http'|| 'https') ? r.imageUrl : process.env.VUE_APP_API_ORIGIN+r.imageUrl
          }
        ]
      }else {
        this.$refs.createModal.fileList = []
      }
      
    },
    // 获取列表
    getList() {
      getMenuTree().then(res => {
        if(res.code == '0') {
          this.data = this.dealMenuList( res.data )
        }
      })
    },
    dealMenuList(list) {
      list.forEach(item => {
        if(!item.children || !item.children.length) {
          delete item.children
        }else {
          this.dealMenuList(item.children)
        }
      })
      return list
    }
  },
  mounted() {
    this.getList()
  }
}
</script>